package com.kk.util.test.thread.join;

import java.util.concurrent.TimeUnit;

/**
 * @function:
 * @author: kevin
 * @date: 2018-09-07 10:39
 */
public class Thread1 extends Thread {

    private String name;

    public Thread1(String name) {
        super(name);
        this.name = name;
    }

    public void run() {
        System.out.println(Thread.currentThread().getName() + "线程运行开始！");
        for (int i = 0; i < 5; i++) {
            System.out.println("子线程" + name + "运行：" + i);
            try {
                int time = (int) (Math.random() * 1000);
                System.out.println(name+"线程睡眠："+time+"ms");
                sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName() + "线程运行结束！");

    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "主线程运行开始！");
        Thread t1 = new Thread1("A");
        Thread t2 = new Thread1("B");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(Thread.currentThread().getName() + "主线程运行结束！");

    }
}
